package com.aaa.service.impl.sas;

import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.entity.mht.MhtPlan;
import com.aaa.entity.sas.SasPlan;
import com.aaa.mapper.mht.MhtPlanMapper;
import com.aaa.mapper.sas.SasPlanMapper;
import com.aaa.service.mht.MhtPlanService;
import com.aaa.service.sas.SasPlanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SasPlanServiceImpl implements SasPlanService {
    @Autowired
    private SasPlanMapper sasPlanMapper;

    /**
     * 添加一个计划
     *
     * @param user
     * @param sasPlan
     * @return
     */
    @Override
    public Boolean addPlan(User user, SasPlan sasPlan) {
        int i = -1;
        Integer id = sasPlan.getId();
        if(id==null){
            Integer uid = user.getId();
            sasPlan.setUid(uid);
            sasPlan.setCreatetime(new Date());
            i = sasPlanMapper.insert(sasPlan);
        }else {
            sasPlan.setUpdatetime(new Date());
            i = sasPlanMapper.updateByPrimaryKeySelective(sasPlan);
        }
        return i>=0;
    }

    /**
     * 查询全部的计划信息 并分页
     * @param myPage
     * @param sasPlan
     * @return
     */
    @Override
    public PageInfo showplan(MyPage myPage, SasPlan sasPlan) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<SasPlan> findplan = sasPlanMapper.findplan(sasPlan);
        PageInfo pageInfo = new PageInfo(findplan);
        return pageInfo;
    }

    /**
     * 修改计划的状态码
     * @param sasPlan
     * @return
     */
    @Override
    public Boolean updPlanstatus(SasPlan sasPlan) {
        int i = sasPlanMapper.updateByPrimaryKeySelective(sasPlan);
        return i>=0;
    }

    /**
     * 检测计划名称是否存在
     * @param planname
     * @return
     */
    @Override
    public Boolean checkPlanname(String planname) {
        SasPlan sasPlan = new SasPlan();
        sasPlan.setPlanname(planname);
        SasPlan sasPlan1 = sasPlanMapper.selectOne(sasPlan);
        if(sasPlan1!=null){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 检测验证码是否存在
     * @param sasPlan
     * @return
     */
    @Override
    public Boolean checkcode(SasPlan sasPlan) {
        SasPlan sasPlan1 = sasPlanMapper.selectOne(sasPlan);
        if(sasPlan1!=null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过验证码获取计划
     * @param sasPlan
     * @return
     */
    @Override
    public SasPlan findPlanByCode(SasPlan sasPlan) {
        SasPlan sasPlan1 = sasPlanMapper.selectOne(sasPlan);
        return sasPlan1;
    }
}
